#include "stdafx.h"
#include "WaitTimeDisplay.h"
#include "DPIForm.h"
#include "FontFactory.h"

IMPLEMENT_DYNAMIC(CWaitTimeDisplay, CTimeDisplay)

CWaitTimeDisplay::CWaitTimeDisplay()
{
	CFontFactory m_fFactory; HDC hScrDC = CreateScrDC();
	HFONT oldFont = (HFONT)::SelectObject(hNumMemDC, m_fFactory.Create(_T("Arial"), 50, 18, FW_BOLD)->GetLast());
	::SetTextColor(hNumMemDC, RGB(0, 255, 0));
	CSize szTime;
	for (intptr_t i = 0; i < 11; i++)
	{
		if (i < 10) swprintf_s(nmTime[i].v, L"%d", (int)i); else wcscpy_s(nmTime[i].v, L"s");
		::GetTextExtentPoint32(hNumMemDC, nmTime[i].v, 1, &nmTime[i].sz);
		if (i < 10) nmTime[i].getSize(i, szTime);
	}
	for (intptr_t i = 0; i < 11; i++)
	{
		if (i < 10)
			drawNum(hScrDC, &nmTime[i], CPoint((szTime.cx - nmTime[i].sz.cx) >> 1, (szTime.cy - nmTime[i].sz.cy) >> 1), szTime);
		else
			drawNum(hScrDC, &nmTime[i], CPoint(0, 0), nmTime[i].sz);
	}
	for (intptr_t i = 0, x = CDPIForm::DPIX(4), y = 0; i < 3; i++)
	{
		numRect[i].left = (int)x;
		if (i == 2)
		{
			x += nmTime[10].sz.cx; numIndex[i] = 10;
			numRect[i].top = (int)(y + ((szTime.cy - nmTime[10].sz.cy) >> 1));
			numRect[i].bottom = numRect[i].top + nmTime[10].sz.cy;
		}
		else
		{
			x += szTime.cx; numIndex[i] = -1;
			numRect[i].top = (int)y;
			numRect[i].bottom = numRect[i].top + szTime.cy;
		}
		numRect[i].right = (int)x;
	}
	::SelectObject(hNumMemDC, oldFont);
	CreateMemBitmap(hScrDC, numRect[2].right + CDPIForm::DPIX(4), szTime.cy);
	::DeleteDC(hScrDC);
	lfSec = -1; drawOut(2, 10);
}

CWaitTimeDisplay::~CWaitTimeDisplay()
{
	for (intptr_t i = 0; i < 11; i++) if (nmTime[i].bmp) ::DeleteObject(nmTime[i].bmp);
}

BEGIN_MESSAGE_MAP(CWaitTimeDisplay, CTimeDisplay)
END_MESSAGE_MAP()

void CWaitTimeDisplay::setWaitTime(intptr_t lastSecond)
{
	if (lastSecond < 0) lastSecond = 0;
	if (lastSecond > 99) lastSecond = 99;
	if (lastSecond == lfSec) return;
	lfSec = lastSecond;
	intptr_t v = (intptr_t)(lastSecond / 10) & 0xf; if (numIndex[0] != v) drawOut(0, v);
	v = lastSecond % 10; if (numIndex[1] != v) drawOut(1, v);
}

void CWaitTimeDisplay::drawOut(intptr_t id, intptr_t n)
{
	numIndex[id] = n;
	if (n >= 0 && n < 11) CTimeDisplay::drawOut(numRect[id], nmTime[n].bmp);
}
